package net.simflow.swf.analyzer.exec.stats.opt;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

import net.simflow.swf.analyzer.network.data.Service;

public class ServiceCountAnalyzer {

	private static ServiceCountAnalyzer instance = new ServiceCountAnalyzer();

	private ServiceCountAnalyzer() {
	}

	public static ServiceCountAnalyzer getInstance() {
		return instance;
	}

	public HashMap<Service, Integer> analyzeTotalCount(
			Collection<? extends Service> serviceSet) {
		HashMap<Service, Integer> map = new HashMap<Service, Integer>();

		if (serviceSet == null) {
			return map;
		}

		for (Service svc : serviceSet) {
			map.put(svc, svc.getTotalCount());
		}

		return map;
	}

	public HashMap<Service, Integer> analyzeExecCount(
			Collection<? extends Service> serviceSet) {
		HashMap<Service, Integer> map = new HashMap<Service, Integer>();

		if (serviceSet == null) {
			return map;
		}

		for (Service svc : serviceSet) {
			map.put(svc, svc.getExecCount());
		}

		return map;
	}

	public HashMap<Service, Integer> analyzeUnfinishCount(
			Collection<? extends Service> serviceSet) {
		HashMap<Service, Integer> map = new HashMap<Service, Integer>();

		if (serviceSet == null) {
			return map;
		}

		for (Service svc : serviceSet) {
			map.put(svc, svc.getUnfinishCount());
		}

		return map;
	}

	public TreeMap<Integer, ArrayList<Service>> analyzeDistribution(
			Map<Service, Integer> serviceCountmap) {
		TreeMap<Integer, ArrayList<Service>> map = new TreeMap<Integer, ArrayList<Service>>();

		if (serviceCountmap == null) {
			return map;
		}

		for (Entry<Service, Integer> en : serviceCountmap.entrySet()) {
			Service svc = en.getKey();
			int count = en.getValue();
			ArrayList<Service> a = map.get(count);
			if (a == null) {
				a = new ArrayList<Service>();
				map.put(count, a);
			}
			a.add(svc);
		}
		map.comparator();

		return map;
	}
}
